home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Inne / Gry / Carnage_Contest / scripts / CC Original / weapons / Crossbow.lua < prev    next >
Text File  |  2010-08-31  |  6KB  |  163 lines

  1. --------------------------------------------------------------------------------
  2. -- Weapon Crossbow + Projectile Bolt
  3. -- Original Carnage Contest Weapon
  4. -- Script by DC, August 2009, www.UnrealSoftware.de
  5. --------------------------------------------------------------------------------
  6.  
  7. -- Setup Tables
  8. if cc==nil then cc={} end
  9. cc.crossbow={}
  10. cc.crossbow.bolt={}
  11.  
  12. -- Load & Prepare Ressources
  13. cc.crossbow.gfx_wpn=loadgfx("weapons/crossbow.bmp")                -- Weapon Image
  14. setmidhandle(cc.crossbow.gfx_wpn)
  15. cc.crossbow.gfx_icon=loadgfx("weapons/crossbowicon.bmp")        -- Weapon Icon
  16. setmidhandle(cc.crossbow.gfx_icon)
  17. cc.crossbow.gfx_pro=loadgfx("weapons/bolt.bmp")                    -- Projectile Image
  18. setmidhandle(cc.crossbow.gfx_pro)
  19. cc.crossbow.sfx_attack=loadsfx("arrow_shoot.ogg")                -- Attack Sound
  20. cc.crossbow.sfx_impact=loadsfx("arrow_impact.ogg")                -- Impact Sound
  21.  
  22. --------------------------------------------------------------------------------
  23. -- Weapon: Crossbow
  24. --------------------------------------------------------------------------------
  25.  
  26. cc.crossbow.id=addweapon("cc.crossbow","Crossbow",cc.crossbow.gfx_icon,1)    -- Add Weapon (1 use)
  27. cc.crossbow.ammo=10                                                -- 10 bolts
  28.  
  29. function cc.crossbow.draw()                                        -- Draw
  30.     setblend(blend_alpha)
  31.     setalpha(1)
  32.     setcolor(255,255,255)
  33.     drawinhand(cc.crossbow.gfx_wpn,6,0)
  34.     -- HUD ammobar
  35.     if cc.crossbow.ammo-weapon_shots>0 then
  36.         hudammobar(cc.crossbow.ammo-weapon_shots,cc.crossbow.ammo)
  37.     end
  38.     -- HUD Crosshair
  39.     if cc.crossbow.ammo-weapon_shots>0 then
  40.         hudcrosshair(7,3)
  41.     end
  42. end
  43.  
  44. function cc.crossbow.attack(attack)                                -- Attack
  45.     -- Decrement timer
  46.     if weapon_timer>0 then
  47.         weapon_timer=weapon_timer-1
  48.     end
  49.     -- Attack
  50.     if weapon_shots<cc.crossbow.ammo and weapon_timer<=0 and attack==1 then
  51.         -- No more weapon switching!
  52.         useweapon(0)
  53.         -- Reset Timer
  54.         weapon_timer=25
  55.         -- Attack
  56.         playsound(cc.crossbow.sfx_attack)
  57.         weapon_shots=weapon_shots+1
  58.         id=createprojectile(cc.crossbow.bolt.id)
  59.         projectiles[id]={}
  60.         -- Ignore collision with current player at beginning
  61.         projectiles[id].ignore=playercurrent()
  62.         -- Set initial position of projectile
  63.         projectiles[id].x=getplayerx(0)+(7*getplayerdirection(0))-math.sin(math.rad(getplayerrotation(0)))*5.0
  64.         projectiles[id].y=getplayery(0)+3+math.cos(math.rad(getplayerrotation(0)))*5.0
  65.         -- Set speed of projectile
  66.         projectiles[id].sx=math.sin(math.rad(getplayerrotation(0)))*14.0
  67.         projectiles[id].sy=-math.cos(math.rad(getplayerrotation(0)))*14.0
  68.         -- Initial movement
  69.         projectiles[id].x=projectiles[id].x-projectiles[id].sx*0.3
  70.         projectiles[id].y=projectiles[id].y-projectiles[id].sy*0.3
  71.         for i=1,1,1 do
  72.             if cc.crossbow.bolt.move(id)==1 then
  73.                 break
  74.             end
  75.         end
  76.         -- Effects
  77.         recoil(3)
  78.         -- End Turn
  79.         if (weapon_shots>=cc.crossbow.ammo) then
  80.             endturn()
  81.         end
  82.     end
  83. end
  84.  
  85. --------------------------------------------------------------------------------
  86. -- Projectile: Bolt
  87. --------------------------------------------------------------------------------
  88.  
  89. cc.crossbow.bolt.id=addprojectile("cc.crossbow.bolt")        -- Add Projectile
  90.  
  91. function cc.crossbow.bolt.draw(id)                            -- Draw
  92.     -- Setup draw mode
  93.     setblend(blend_alpha)
  94.     setalpha(1)
  95.     setcolor(255,255,255)
  96.     setscale(1,1)
  97.     -- Calculate projectile rotation
  98.     setrotation(math.deg(math.atan2(projectiles[id].sx,-projectiles[id].sy)))
  99.     -- Draw projectile
  100.     drawimage(cc.crossbow.gfx_pro,projectiles[id].x,projectiles[id].y)
  101.     -- Draw arrow if out of Screen
  102.     outofscreenarrow(projectiles[id].x,projectiles[id].y)
  103. end
  104.  
  105. function cc.crossbow.bolt.update(id)                        -- Update
  106.     -- Wind + Gravity influence on speed
  107.     projectiles[id].sx=projectiles[id].sx+getwind()*0.1
  108.     projectiles[id].sy=projectiles[id].sy+getgravity()*1.5
  109.     -- Move
  110.     cc.crossbow.bolt.move(id)
  111. end
  112.  
  113. function cc.crossbow.bolt.move(id)
  114.     rot=math.deg(math.atan2(projectiles[id].sx,-projectiles[id].sy))
  115.     -- Move (in substep loop for optimal collision precision)
  116.     msubt=math.ceil(math.max(math.abs(projectiles[id].sx),math.abs(projectiles[id].sy))/2)
  117.     msubx=projectiles[id].sx/msubt
  118.     msuby=projectiles[id].sy/msubt
  119.     for i=1,msubt,1 do
  120.         projectiles[id].x=projectiles[id].x+msubx
  121.         projectiles[id].y=projectiles[id].y+msuby        
  122.         -- Collision
  123.         if collision(col2x2,projectiles[id].x+math.sin(math.rad(rot))*4,projectiles[id].y-math.cos(math.rad(rot))*4)==1 then
  124.             if terraincollision()==1 or objectcollision()>0 or playercollision()~=projectiles[id].ignore then
  125.                 if playercollision()~=0 then
  126.                     -- Cause Player damage
  127.                     playerpush(playercollision(),projectiles[id].sx/10.0,projectiles[id].sy/10.0)
  128.                     playerdamage(playercollision(),3)
  129.                     blood(projectiles[id].x+math.sin(math.rad(rot))*4,projectiles[id].y-math.cos(math.rad(rot))*4)
  130.                     playsound(sfx_splatter3)
  131.                 elseif objectcollision()>0 then
  132.                     -- Cause Object damage
  133.                     objectdamage(objectcollision(),3)
  134.                 else
  135.                     -- Draw bolt in terrain
  136.                     terrainimage(cc.crossbow.gfx_pro,projectiles[id].x,projectiles[id].y,0,rot)
  137.                 end
  138.                 -- Effects
  139.                 playsound(cc.crossbow.sfx_impact)
  140.                 particle(p_smoke,projectiles[id].x+math.sin(math.rad(rot))*4,projectiles[id].y-math.cos(math.rad(rot))*4)
  141.                 particlefadealpha(0.006)
  142.                 -- Free projectile
  143.                 freeprojectile(id)
  144.                 return 1
  145.             end
  146.         else
  147.             projectiles[id].ignore=0
  148.         end
  149.         -- Water
  150.         if (projectiles[id].y)>getwatery()+5 then
  151.             -- Effects
  152.             particle(p_waterhit,projectiles[id].x,projectiles[id].y)
  153.             if math.random(1,2)==1 then
  154.                 playsound(sfx_hitwater2)
  155.             else
  156.                 playsound(sfx_hitwater3)
  157.             end
  158.             -- Free projectile
  159.             freeprojectile(id)
  160.             return 1
  161.         end
  162.     end
  163. end